import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/input/input_field.dart';
import 'package:flutter/material.dart';

import 'default_bottom_sheet.dart';

/// 添加折扣
/// delete  返回 true
/// ok 返回对应的折扣
showAddPromoCodeBottomSheet(BuildContext context, String value,
    Future<bool> Function(String value) add) async {
  final result = await showDefaultBottomSheet(context,
      child: AddPromoCodeBottomSheet(
        value: value,
        add: add,
      ),
      title: S.current.promo_code,
      isScrollControlled: true,
      leading: value != null
          ? DefaultBottomSheetLeadingButton(
              title: S.current.remove,
              onTap: value == null
                  ? null
                  : () {
                      Navigator.of(context).pop(true);
                    },
            )
          : null);
  return result;
}

class AddPromoCodeBottomSheet extends StatefulWidget {
  final String value;
  final Future<bool> Function(String value) add;

  AddPromoCodeBottomSheet({Key key, this.value, this.add}) : super(key: key);

  @override
  _AddPromoCodeBottomSheetState createState() =>
      _AddPromoCodeBottomSheetState();
}

class _AddPromoCodeBottomSheetState extends State<AddPromoCodeBottomSheet> {
  TextEditingController promoCodeController = TextEditingController();
  FocusNode promoCodeFocusNode = FocusNode();
  bool isEnable = false;
  bool hasError = false;

  void _onAdd() async {
    promoCodeFocusNode.unfocus();
    if (promoCodeController.text.length == 0) {
      BaseConfig.dialogBase.showToast(
          context: context, content: S.current.please_enter_the_promo_code);
      return;
    }
    String returnText = promoCodeController.text;
    bool isSuccess = await widget.add.call(returnText);
    if (isSuccess == true) {
      Navigator.of(context).pop();
    } else {
      setState(() {
        hasError = true;
      });
    }
  }

  @override
  void initState() {
    super.initState();
    if (widget.value != null) {
      promoCodeController.text = widget.value;
    }
    promoCodeController.addListener(() {
      if (promoCodeController.text.length > 0) {
        if (isEnable == false) {
          setState(() {
            isEnable = true;
          });
        }
      } else {
        if (isEnable == true) {
          setState(() {
            isEnable = false;
          });
        }
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    promoCodeController.dispose();
    promoCodeFocusNode.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        AppSpaces.h20,
        InputField(
          tip: S.current.promo_code,
          controller: promoCodeController,
          focusNode: promoCodeFocusNode,
          hasError: hasError,
        ),
        AppSpaces.h10,
        Center(
          child: Visibility(
              visible: hasError,
              child: Text(
                S.current.promo_code_is_invalid_has_expired,
                style: TextStyle(
                  color: aceRedColor,
                  fontSize: getSp(14),
                  fontWeight: FontWeight.w700,
                  height: 16 / 14,
                ),
              )),
        ),
        AppSpaces.h30,
        NormalButton(
          title: S.current.apply,
          onTap: isEnable ? _onAdd : null,
        ),
      ],
    );
  }
}
